/**
 * Created by tomi on 1/30/15.
 */
var express = require('express');
var router = express.Router();
var Appointment = require('../model/appointment');
var Response = require('../util/ResponseUtil');
var _ = require('lodash');

/* get my appointments */
router.get('/', function (req, res, next) {
    var loginUser = req.session.loginUser;

    Appointment.find({'sponsor': loginUser._id}, function (err, data) {
        res.json(data);
    });

});

router.get('/accepted', function (req, res, next) {
    var loginUser = req.session.loginUser;

    Appointment.find({'attendees': loginUser._id}, function (err, data) {
        res.json(data);
    });
});

/* add new apppointment */
router.post('/', function (req, res, next) {
    var loginUser = req.session.loginUser;
    var newAppointment = req.param('appointment');

    newAppointment.sponsor = loginUser._id;
    console.log(newAppointment);

    new Appointment(newAppointment).save(function (err, result) {
        if (err) {
            console.log(err);
        }
        res.send(result);
    });

});


router.put('/', function (req, res, next) {

    var newAppo = req.param('appointment');

    console.log(newAppo);

    Appointment.update({'_id': newAppo.id}, newAppo, function (err, appo) {
        if (err) {
            console.log(err);
            Response.ERROR(res);
            return;
        }

        Response.OK(res, appo);
    });

});

router.delete('/', function (req, res, next) {

    Appointment.remove({}, function (err) {
        if (err) {
            console.log(err);
        }

        Response.OK(res);
    });
});

router.delete('/:id', function (req, res, next) {

    var id = req.params.id;

    Appointment.remove({_id: id}, function (err) {
        if (err) {
            console.log(err);
        }

        Response.OK(res);
    });
});


module.exports = router;